package com.xujl.fastlib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.xujl.fastlib.R;
import com.xujl.fastlib.base.IStateLayout;

/**
 * 多状态布局
 */
public class StateLayout extends FrameLayout implements IStateLayout {

    /**
     * 空数据View
     */
    private View mEmptyView;
    /**
     * 错误View
     */
    private View mErrorView;
    /**
     * 加载View
     */
    private View mLoadingView;
    private SparseArray<OnClickListener> mListenerMap = new SparseArray<>(3);

    public StateLayout (Context context) {
        this(context, null);
    }

    public StateLayout (Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public StateLayout (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void dismissAll () {
        if (mEmptyView != null) {
            mEmptyView.setVisibility(GONE);
        }
        if (mErrorView != null) {
            mErrorView.setVisibility(GONE);
        }
        if (mLoadingView != null) {
            mLoadingView.setVisibility(GONE);
        }
    }

    private void showLoading () {
        setVisibility(VISIBLE);
        dismissAll();
        if (mLoadingView == null) {
            mLoadingView = LayoutInflater.from(getContext()).inflate(R.layout.base_loading, null);
            mLoadingView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            addView(mLoadingView);
        }
        mLoadingView.setVisibility(VISIBLE);

    }

    private void showEmpty () {
        setVisibility(VISIBLE);
        dismissAll();
        if (mEmptyView == null) {
            mEmptyView = LayoutInflater.from(getContext()).inflate(R.layout.base_empty, null);
            mEmptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            addView(mEmptyView);
        }
        bindListener(TYPE_EMPTY);
        mEmptyView.setVisibility(VISIBLE);

    }

    private void showError () {
        setVisibility(VISIBLE);
        dismissAll();
        if (mErrorView == null) {
            mErrorView = LayoutInflater.from(getContext()).inflate(R.layout.base_error, null);
            mErrorView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            addView(mErrorView);
        }
        bindListener(TYPE_ERROR);
        mErrorView.setVisibility(VISIBLE);
    }

    private void showContent () {
        setVisibility(GONE);
    }

    private void bindListener (@StateType int type) {
        switch (type) {
            case TYPE_LOADING:
                break;
            case TYPE_EMPTY:
                if (mEmptyView != null) {
                    mEmptyView.findViewById(R.id.btn_try).setOnClickListener(mListenerMap.get(TYPE_EMPTY));
                }
                break;
            case TYPE_ERROR:
                if (mErrorView != null) {
                    mErrorView.findViewById(R.id.btn_try).setOnClickListener(mListenerMap.get(TYPE_ERROR));
                }
                break;
            case TYPE_SUCCESS:
                break;
            default:
                break;

        }
    }

    @Override
    public IStateLayout setStateView (@StateType int type, View view) {
        switch (type) {
            case TYPE_LOADING:
                mLoadingView = view;
                mLoadingView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                mLoadingView.setVisibility(GONE);
                addView(mLoadingView);
                break;
            case TYPE_EMPTY:
                mEmptyView = view;
                mEmptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                mEmptyView.setVisibility(GONE);
                addView(mEmptyView);
                break;
            case TYPE_ERROR:
                mErrorView = view;
                mErrorView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                mErrorView.setVisibility(GONE);
                addView(mErrorView);
                break;
            default:
                break;

        }
        return this;
    }

    @Override
    public IStateLayout setStateListener (@StateType int type, OnClickListener listener) {
        mListenerMap.put(type, listener);
        bindListener(type);
        return this;
    }

    @Override
    public void showState (@StateType int type) {
        switch (type) {
            case TYPE_LOADING:
                showLoading();
                break;
            case TYPE_EMPTY:
                showEmpty();
                break;
            case TYPE_ERROR:
                showError();
                break;
            case TYPE_SUCCESS:
                showContent();
                break;
            default:
                showContent();
                break;

        }
    }
}
